package com.fly.estore.dao.Impl;

import com.fly.estore.dao.BaseDAO;
import com.fly.estore.dao.ProductBrandDAO;
import com.fly.estore.domain.ProductBrand;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fly on 2016/4/16.
 */
@Repository("ProductBrandDAO")
public class ProductBrandDAOImpl extends BaseDAO<ProductBrand> implements ProductBrandDAO {

    public int addProductBrand(final ProductBrand brand) {
        int update = 0;
        StringBuilder sql = new StringBuilder();
        sql.append("insert into product_brand(product_brand_id, brand_name, brand_logo, brand_intro, create_id, create_time)")
                .append("values (?, ?, ?, ?, ?, ?)");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, brand.getProductBrandId());
                ps.setString(i++, brand.getBrandName());
                ps.setString(i++, brand.getBrandLogo());
                ps.setString(i++, brand.getBrandIntro());
                ps.setString(i++, brand.getCreateId());
                ps.setString(i++, brand.getCreateTime());
            }
        });
        return update;
    }

    public int updateProductBrand(final ProductBrand brand) {
        int update = 0;
        StringBuilder sql = new StringBuilder();
        sql.append("update product_brand set ")
                .append(" brand_name = ?,")
                .append(" brand_logo = ?,")
                .append(" brand_intro = ?,")
                .append(" last_update_id = ?,")
                .append(" last_update_time = ?")
                .append(" where product_brand_id = ?");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, brand.getBrandName());
                ps.setString(i++, brand.getBrandLogo());
                ps.setString(i++, brand.getBrandIntro());
                ps.setString(i++, brand.getLastUpdateId());
                ps.setString(i++, brand.getLastUpdateTime());
                ps.setString(i++, brand.getProductBrandId());
            }
        });
        return update;
    }

    public int deleteById(final String productBrandId) {
        int update = 0;
        StringBuilder sql = new StringBuilder();
        sql.append("delete from product_brand where product_brand_id = ?");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, productBrandId);
            }
        });
        return update;
    }

    public ProductBrand getById(String productBrandId) {
        ProductBrand brand = null;
        StringBuilder sql = new StringBuilder();
        sql.append("select * from product_brand where product_brand_id = ? order by create_time");
        List<ProductBrand> resultList = getJdbcTemplate().query(sql.toString(), new BrandRowMapper(), new Object[]{productBrandId});
        if (!CollectionUtils.isEmpty(resultList)){
            brand = resultList.get(0);
        }
        return brand;
    }

    public List<ProductBrand> getList() {
        List<ProductBrand> brandList = new ArrayList<ProductBrand>();
        StringBuilder sql = new StringBuilder();
        sql.append("select * from product_brand");
        brandList = getJdbcTemplate().query(sql.toString(), new BrandRowMapper());
        return brandList;
    }

    public class BrandRowMapper implements RowMapper<ProductBrand>{
        public ProductBrand mapRow(ResultSet rs, int rowNum) throws SQLException {
            ProductBrand brand = new ProductBrand();
            brand.setProductBrandId(rs.getString("product_brand_id"));
            brand.setBrandName(rs.getString("brand_name"));
            brand.setBrandLogo(rs.getString("brand_logo"));
            brand.setBrandIntro(rs.getString("brand_intro"));
            brand.setCreateId(rs.getString("create_id"));
            brand.setCreateTime(rs.getString("create_time"));
            brand.setLastUpdateId(rs.getString("last_update_id"));
            brand.setLastUpdateTime(rs.getString("last_update_time"));
            return brand;
        }
    }
}
